﻿using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using FileSynchronizer.DataAccess;
using mylibrary;

namespace FileSynchronizer.Objects
{
    //---------------------------------------------------------------------------------------------------------------------------------------
    // Code based upon the State Pattern, as described by the GOF
    //
    // http://en.wikipedia.org/wiki/State_pattern and http://en.wikipedia.org/wiki/Talk:State_pattern#Disputed
    //
    // This is a Concrete State Object
    //
    //---------------------------------------------------------------------------------------------------------------------------------------
    
    public class ProgramState_Idle : ProgramState
    {
        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public ProgramState_Idle(ProgramContext TheProgramContext)
            : base(TheProgramContext)
        {
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public Methods
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Delete a FolderPair
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool DeleteFolderPair(FolderPair TheFolderPair, SyncDatabaseHelper TheSyncDatabaseHelper)
        {
            string myMetaDataFileName = "";
            string myLocalApplicationDataPath = "";
            string myFileToDelete = "";
            bool OperationResult = true;

            try
            {
                myLocalApplicationDataPath = ApplicationAttributes.LocalApplicationDataPath;
                //Remove Metadata files
                myMetaDataFileName = string.Concat(TheFolderPair.LeftIdentifier.ToString(), FileSyncHelperClass.MetaDataFileExtension);
                myFileToDelete = Path.Combine(myLocalApplicationDataPath, myMetaDataFileName);
                if (File.Exists(myFileToDelete))
                {
                    File.Delete(myFileToDelete);
                }
                myMetaDataFileName = string.Concat(TheFolderPair.RightIdentifier.ToString(), FileSyncHelperClass.MetaDataFileExtension);
                myFileToDelete = Path.Combine(myLocalApplicationDataPath, myMetaDataFileName);
                if (File.Exists(myFileToDelete))
                {
                    File.Delete(myFileToDelete);
                }

                // remove from database
                TheSyncDatabaseHelper.DeleteFolderPair(TheFolderPair);
                // remove from lists
                //LoadFolderPairs();
            }
            catch (Exception ex)
            {
                //FormHelperClass.ProcessUnhandledException(ex, ApplicationTitle);
                OperationResult = false;
            }

            return OperationResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Synchronize a FolderPair
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool SynchronizeFolderPair(FolderPair TheFolderPair)
        {
            bool OperationResult = true;
            ProgramState NextState;

            if (TheFolderPair != null)
            {
                if (CurrentProgramContext.Settings.EnableEnterpriseFeatures)
                {
                    NextState = new ProgramState_Enterprise_SyncFolderPair(CurrentProgramContext);
                }
                else
                {
                    NextState = new ProgramState_SyncFolderPair(CurrentProgramContext);
                }
                ChangeState(NextState);
                CurrentProgramContext.CurrentState = State.SyncFolderPair;
                OperationResult = NextState.SynchronizeFolderPair(TheFolderPair);
            }
            return OperationResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Synchronize all FolderPairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool SynchronizeAllFolderPairs(IQueryable<FolderPair> TheFolderPairs, RunWorkerCompletedEventHandler ExternalCompletionHandler)
        {
            bool OperationResult = false;
            ProgramState NextState;

            if (TheFolderPairs != null)
            {
                if (CurrentProgramContext.Settings.EnableEnterpriseFeatures)
                {
                    NextState = new ProgramState_Enterprise_SyncAllFolderpairs(CurrentProgramContext);
                }
                else
                {
                    NextState = new ProgramState_SyncAllFolderpairs(CurrentProgramContext);
                }
                ChangeState(NextState);
                CurrentProgramContext.CurrentState = State.SyncAllFolderPairs;
                OperationResult = NextState.SynchronizeAllFolderPairs(TheFolderPairs, ExternalCompletionHandler);
            }
            else
            {
                SyncFolderPairListCompleted(null, null);
            }
            return OperationResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Callback Function which is called whenever the synchronization of ALL folderpairs is complete
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void SyncFolderPairListCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //ChangeState(new ProgramState_Idle(CurrentProgramContext));
            //CurrentProgramContext.CurrentState = State.Idle;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }
}

